﻿using System;
using System.Reflection;
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person()
    {
        Name = "Unknown";
        Age = 0;
    }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    public void Introduce()
    {
        Console.WriteLine($"Hi, my name is {Name}, and I am {Age} years old.");
    }
}

class Program
{
    static void Main()
    {
        Type personType = typeof(Person);

        // 无参
        object person1 = Activator.CreateInstance(personType);
        Person p1 = (Person)person1;
        p1.Introduce(); 

        //有参
        object[] parameters = { "John", 30 };
        object person2 = Activator.CreateInstance(personType, parameters);
        Person p2 = (Person)person2;
        p2.Introduce(); 

        // 通过反射获取构造方法并手动调用
        ConstructorInfo constructorInfo = personType.GetConstructor(new Type[] { typeof(string), typeof(int) });
        object person3 = constructorInfo.Invoke(new object[] { "Alice", 25 });
        Person p3 = (Person)person3;
        p3.Introduce(); 
    }
}